package m202405.d11;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * @@author: 爱做梦的锤子
 * @date: 2024/5/11 14:19
 */
public class Q438 {

    public static void main(String[] args) {
        Q438 q438 = new Q438();
        q438.findAnagrams("cbaebabacd", "abc");
    }

    public List<Integer> findAnagrams(String s, String p) {
        if (s.length() < p.length()) {
            return new ArrayList<>();
        }

        int[] key = new int['z' - 'a' + 1];
        int[] ck = new int['z' - 'a' + 1];
        for (int i = 0; i < p.length(); i++) {
            key[p.charAt(i) - 'a'] += 1;
            ck[s.charAt(i) - 'a'] += 1;
        }
        List<Integer> rs = new ArrayList<>();
        if (Arrays.equals(key, ck)) {
            rs.add(0);
        }
        for (int i = 0; i < s.length() - p.length() + 1; i++) {
            ck[s.charAt(i) - 'a'] -= 1;
            ck[s.charAt(i + p.length()) - 'a'] += 1;
            if (Arrays.equals(key, ck)) {
                rs.add(i+1);
            }
        }
        return rs;
    }


}
